﻿var CurrentMenu = {};

Ext.onReady(function () {
    PageSafe();

    var MenuTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 530,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(MenuTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    CurrentMenu = node.attributes;
                }
            }
        }
    });

    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '菜单列表',
            iconCls: 'menu-icon',
            tbar: [{ text: '新增菜单', iconCls: 'add-icon', handler: function () {
                var addwin = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 330,
                    title: '新增菜单',
                    iconCls: 'menu-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '父菜单',
                        width: 100,
                        id: 'ParentName',
                        name: 'ParentName'
                    }, {
                        fieldLabel: '菜单名称',
                        width: 100,
                        id: 'MenuName',
                        name: 'MenuName'
                    }, {
                        fieldLabel: '菜单指向路径',
                        width: 200,
                        id: 'MenuUrl',
                        name: 'MenuUrl'
                    }, {
                        fieldLabel: '菜单显示区域',
                        width: 100,
                        id: 'MenuFm',
                        name: 'MenuFm'
                    }, {
                        fieldLabel: '菜单图片路径',
                        width: 200,
                        id: 'MenuImg',
                        name: 'MenuImg'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("MenuName").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单名称"); return; }
                        if (Ext.getCmp("MenuUrl").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单链接地址"); return; }
                        if (Ext.getCmp("MenuFm").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单显示区域"); return; }
                        if (Ext.getCmp("MenuImg").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单图片路径"); return; }


                        var re = new JsonRequest();
                        if (CurrentMenu.id == null) {
                            re.params.info = {
                                MenuName: Ext.getCmp("MenuName").getValue(),
                                MenuUrl: Ext.getCmp("MenuUrl").getValue(),
                                MenuFm: Ext.getCmp("MenuFm").getValue(),
                                MenuImg: Ext.getCmp("MenuImg").getValue(),
                                ParentID: null
                            }
                        }
                        else {
                            re.params.info = {
                                MenuName: Ext.getCmp("MenuName").getValue(),
                                MenuUrl: Ext.getCmp("MenuUrl").getValue(),
                                MenuFm: Ext.getCmp("MenuFm").getValue(),
                                MenuImg: Ext.getCmp("MenuImg").getValue(),
                                ParentID: CurrentMenu.id
                            }
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.RoleView', 'AddMenu'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("菜单管理提示", "菜单增加成功", function () { renderTree(MenuTree); addwin.close(); });
                                }
                                else {
                                    Ext.Msg.alert("菜单管理提示", "菜单增加失败，请联系管理员", function () { renderTree(MenuTree); addwin.close(); });
                                }
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addwin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentMenu.id != null) {
                                Ext.getCmp("ParentName").setValue(CurrentMenu.text);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '修改菜单', iconCls: 'update-icon', handler: function () {
                if (CurrentMenu.id == null) { Ext.Msg.alert("提示信息", "请选择需要修改的菜单"); return; }
                var upwin = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 330,
                    title: '修改菜单',
                    iconCls: 'menu-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '菜单名称',
                        width: 100,
                        id: 'u_MenuName',
                        name: 'u_MenuName'
                    }, {
                        fieldLabel: '菜单指向路径',
                        width: 200,
                        id: 'u_MenuUrl',
                        name: 'u_MenuUrl'
                    }, {
                        fieldLabel: '菜单显示区域',
                        width: 100,
                        id: 'u_MenuFm',
                        name: 'u_MenuFm'
                    }, {
                        fieldLabel: '菜单图片路径',
                        width: 200,
                        id: 'u_MenuImg',
                        name: 'u_MenuImg'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("u_MenuName").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单名称"); return; }
                        if (Ext.getCmp("u_MenuUrl").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单链接地址"); return; }
                        if (Ext.getCmp("u_MenuFm").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单显示区域"); return; }
                        if (Ext.getCmp("u_MenuImg").getValue() == "") { Ext.Msg.alert("提示信息", "请输入菜单图片路径"); return; }


                        var re = new JsonRequest();

                        re.params.info = {
                            MenuName: Ext.getCmp("u_MenuName").getValue(),
                            MenuUrl: Ext.getCmp("u_MenuUrl").getValue(),
                            MenuFm: Ext.getCmp("u_MenuFm").getValue(),
                            MenuImg: Ext.getCmp("u_MenuImg").getValue(),
                            ParentID: CurrentMenu.parentId,
                            PID: CurrentMenu.id,
                            OrderID: CurrentMenu.OrderID,
                            ClassName: CurrentMenu.ClassName,
                            NameID: CurrentMenu.NameID
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.RoleView', 'UpdateMenu'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("菜单管理提示", "菜单修改成功", function () { renderTree(MenuTree); upwin.close(); });
                                }
                                else {
                                    Ext.Msg.alert("菜单管理提示", "菜单修改失败，请联系管理员", function () { renderTree(MenuTree); upwin.close(); });
                                }
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentMenu.id != null) {
                                Ext.getCmp("u_MenuName").setValue(CurrentMenu.text);
                                Ext.getCmp("u_MenuUrl").setValue(CurrentMenu.MenuUrl);
                                Ext.getCmp("u_MenuFm").setValue(CurrentMenu.MenuFm);
                                Ext.getCmp("u_MenuImg").setValue(CurrentMenu.ImgUrl);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '删除菜单', iconCls: 'del-icon', handler: function () {
                if (CurrentMenu.id == null) { Ext.Msg.alert("提示信息", "请选择需要删除的菜单"); return; }
            } 
            }],
            items: MenuTree
        }]
    });
});

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.RoleView', 'GetMenus'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.ChildMenu = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部菜单'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.ChildMenu != null && o.ChildMenu.length > 0) {
                    for (var a = 0; a < o.ChildMenu.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.ChildMenu[a].PID,
                            icon: o.ChildMenu[a].SysImg,
                            text: o.ChildMenu[a].MenuName,
                            parentId: o.ChildMenu[a].ParentID,
                            MenuUrl: o.ChildMenu[a].MenuUrl,
                            MenuFm: o.ChildMenu[a].MenuFm,
                            ImgUrl: o.ChildMenu[a].MenuImg,
                            OrderID: o.ChildMenu[a].OrderID,
                            ClassName: o.ChildMenu[a].ClassName,
                            NameID: o.ChildMenu[a].NameID,
                            checked: false
                        });
                        node.appendChild(n);
                        appendChild(n, o.ChildMenu[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}